package com.itcam.phaselist.set_;

import java.util.HashSet;

public class HashSet01 {
    public static void main(String[] args) {
        HashSet set = new HashSet();
        //说明
        //1. 在执行add方法后，会返回一个boolean值
        //2. 如果添加成功，返回true, 否则返回false
        //3. 可以通过remove指定删除哪个对象
        System.out.println(set.add("john"));//T
        System.out.println(set.add("lucy"));//T
        System.out.println(set.add("john"));//F
        System.out.println(set.add("jack"));//T
        System.out.println(set.add("Rose"));//T

        set.remove("john");
        System.out.println("set = " + set);// 3个

        set = new HashSet();
        System.out.println("set = " + set);// 0个

        //4.Hashset不能添加相同的元素/数据
        set.add("lucy");//添加成功
        set.add("lucy");//加入不了
        set.add(new Dog("tom"));//OK
        set.add(new Dog("tom"));//Ok
        System.out.println("set = " + set);

        set.add(new String("kim"));
        set.add(new String("kim"));
        System.out.println("set = " + set);

        System.out.println(new Dog("tom").equals(new Dog("tom")));
        System.out.println(new String("kim").equals(new String("kim")));

    }
}

class Dog { //定义了Dog类
    private String name;

    public Dog(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                '}';
    }
}
